package com.bingxue.edu.tools;

import com.bingxue.edu.common.enums.JoinTypeEnum;
import com.bingxue.edu.management.education.model.entity.Lesson;
import com.bingxue.edu.management.education.model.entity.LessonStudent;
import com.bingxue.edu.management.education.service.ClassCancelService;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.EnrollChangeRecordService;
import com.bingxue.edu.management.education.service.EnrollmentService;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.management.education.service.LessonStudentService;
import com.bingxue.edu.management.shop.service.OrderItemService;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.management.shop.service.RefundService;
import com.bingxue.edu.framework.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.LessonStudentTableDef.LESSON_STUDENT;

/**
 * 处理重复缴费导致的订单状态异常
 * @author panbo
 * @since 2024/11/23
 */
@SpringBootTest
public class LessonClassIdFixedTool {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private LessonStudentService lessonStudentService;
    @Autowired
    private EnrollmentService enrollmentService;
    @Autowired
    private EnrollChangeRecordService enrollChangeRecordService;
    @Autowired
    private RefundService refundService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private ClassCancelService classCancelService;
    @Autowired
    private LessonService lessonService;

//    @Test
//    @Transactional(rollbackFor = Exception.class)
    public void fixed() {
        List<LessonStudent> records = lessonStudentService.queryChain()
//                .leftJoin(LESSON).on(LESSON_STUDENT.LESSON_ID.eq(LESSON.ID))
                .where(LESSON_STUDENT.LESSON_CLASS_ID.likeLeft("5"))
                .list();
        System.out.println("record count: " + records.size());
        int i = 1;
        for (LessonStudent record : records) {
            Lesson lesson = lessonService.getById(record.getLessonId());
            record.setLessonClassId(lesson.getClassId());
            if (record.getStudentJoinType() == JoinTypeEnum.CHANGE_CLASS) {
               if(!record.getLessonClassId().equals(record.getStudentClassId())) {
                   System.out.printf("转班数据错误，lesson student id: %s, lesson class: %s, student class: %s%n", record.getId(), record.getLessonClassId(), record.getStudentClassId());
                   throw new BusinessException("转班数据错误");
               }
            } else if (record.getStudentJoinType() == JoinTypeEnum.CHANGE_LESSON) {
                if(record.getLessonClassId().equals(record.getStudentClassId())) {
                    System.out.printf("调课数据错误，lesson student id: %s, lesson class: %s, student class: %s%n", record.getId(), record.getLessonClassId(), record.getStudentClassId());
                    throw new BusinessException("调课数据错误");
                }
            }
            lessonStudentService.updateChain()
                            .set(LESSON_STUDENT.LESSON_CLASS_ID, lesson.getClassId())
                            .where(LESSON_STUDENT.ID.eq(record.getId()))
                            .update();
            System.out.printf("%d/%d%n", i, records.size());
            i++;
//            lessonStudentService.updateById(record);
        }
    }


}
